clear
set more off
set type double
graph drop _all
program drop _all
set scheme s2mono
log using "\\file\UsersW$\wrr15\Home\My Documents\My Files\EFFECTS OF SPILLOVERS ON EXPORTS_A META-ANALYSIS\DATAVERSE FILES\Part1Results.smcl", replace
use "\\file\UsersW$\wrr15\Home\My Documents\My Files\EFFECTS OF SPILLOVERS ON EXPORTS_A META-ANALYSIS\DATAVERSE FILES\meta-orig.dta", clear

rename spill_other_firm all
rename idstudy id
rename sameregion spill_reg
rename sameindustry spill_ind
rename spill_exporter spill_ex
rename spill_value value
rename spill_employment employment
rename spill_output output
rename spill_number number
rename spill_asset asset
rename spill_rd rdsp
rename spill_other other_ms
rename olsgls ogls
rename probit plt
rename nonspherical nsph
rename endogeneity endg
rename categorical catg
rename sampleselection sps
rename panel_fe fe
rename aggregate agg
rename crosssection cs
rename labourquality labq
rename productivity prod
rename publicationyear pubyear

replace all = 1 if foreign ==1
drop foreign
rename all nondomestic
replace other_ms = 1 if asset==1
drop asset
replace impact = 0 if impact ==.
gen firm_level = 1 if agg == 0
replace firm_level = 0 if agg == 1

******************************************************************************
*DEFINITION OF BASIC VARIABLES
******************************************************************************
gen tstat = t
gen df = samplesize - k
gen r = tstat/sqrt(tstat^2+df)
gen varR = (1-r^2)/df
gen seR = sqrt(varR)
gen pcc = r
gen avgyear=(end+start)/2
gen tspan= end-start+1
gen abststat = abs(tstat)
gen abspcc = abs(pcc)


*--------------*
*   TABLE 1    *
*--------------*

******************************************************************************
* Kick out extreme values of PCC
******************************************************************************
quietly summ pcc, detail
keep if pcc > r(p1) & pcc < r(p99) 

summ tstat, detail
summ df, detail 
summ pcc, detail


******************************************************************************
* Characteristics of the data
******************************************************************************

// Spillover mechanisms
summ spill_ex spill_reg spill_ind spill_fdi 

// Spillover measures
summ number value employment output rdsp other_ms

// Dependent variable
summ binary 

// Data characteristics
summ firm_level domestic avgyear 

// Countries
summ oecd eu developing china

// Industry group
summ manufacturing service it food other_industry

// Estimation type
gen other_est = 1 - ogls - plt
summ plt ogls other_est

// Estimation type - endogeneity
//gen endg_sps = (endg == 1 & sps == 1)
summ endg sps fe 
//drop endg_sps

// Control variables
summ size prod labq capital rd

// Study characteristics
summ published impact citation

clear

use "\\file\UsersK$\kkd17\Home\My Documents\UC\Supervision\Jianhua Duan\ROWE\Replication Data\meta-orig.dta", clear


rename spill_other_firm all
rename idstudy id
rename sameregion spill_reg
rename sameindustry spill_ind
rename spill_exporter spill_ex
rename spill_value value
rename spill_employment employment
rename spill_output output
rename spill_number number
rename spill_asset asset
rename spill_rd rdsp
rename spill_other other_ms
rename olsgls ogls
rename probit plt
rename nonspherical nsph
rename endogeneity endg
rename categorical catg
rename sampleselection sps
rename panel_fe fe
rename aggregate agg
rename crosssection cs
rename labourquality labq
rename productivity prod
rename publicationyear pubyear

replace all = 1 if foreign ==1
drop foreign
rename all nondomestic
replace other_ms = 1 if asset==1
drop asset
replace impact = 0 if impact ==.
gen firm_level = 1 if agg == 0
replace firm_level = 0 if agg == 1

******************************************************************************
*DEFINITION OF BASIC VARIABLES
******************************************************************************
gen tstat = t
gen df = samplesize - k
gen r = tstat/sqrt(tstat^2+df)
gen varR = (1-r^2)/df
gen seR = sqrt(varR)
gen pcc = r
gen avgyear=(end+start)/2
gen tspan= end-start+1
gen abststat = abs(tstat)
gen abspcc = abs(pcc)

*--------------*
*   TABLE 2    *
*--------------*

******************************************************************************
* Distribution of t-stats and PCCs before kicking out outliers
******************************************************************************
summ tstat, detail
summ df, detail 
summ pcc, detail

******************************************************************************
* Kick out extreme values of PCC
******************************************************************************
quietly summ pcc, detail
keep if pcc > r(p1) & pcc < r(p99) 

summ tstat, detail
summ df, detail 
summ pcc, detail

*--------------*
*   FIGURE 1   *
*--------------*

******************************************************************************
* Characteristics of t-stats and PCCs of final data set
******************************************************************************
histogram tstat, bin(80) name(histTSTAT)
gen tstat1 = (tstat<-2)
gen tstat2 = (tstat>=-2 & tstat<=2)
gen tstat3 = (tstat>2)
// NOTE: A lot of insignificant t-stats
sum tstat1 tstat2 tstat3

histogram pcc, bin(80) name(histPCC)



******************************************************************************
* Time Series of Median Study PCC 
******************************************************************************
//bysort id: egen PCCmed = median(pcc)
//graph twoway (scatter PCCmed pubyear, msize(*1.5) msymbol(Oh)) (lfit PCCmed pubyear)


*--------------*
*   TABLE 3    *
*--------------*

/*
******************************************************************************
* Determining the weight of individual studies (Fixed Effects)
******************************************************************************
// Three studies account for approximately 79% of the weight
// As a result, prefer Random Effects in subsequent analysis
gen fevarR = varR
gen feweightR = 1/fevarR
gen rR = r*feweightR
collapse (sum) rR feweightR, by (id)
gen avgR = rR/feweightR
gen avgsterrR = sqrt(1/feweightR)
sort id
metan avgR avgsterrR, fixed lcols(id) nobox
gen studyweight = _WT
summ studyweight, detail
gsort -studyweight
*/

/*
******************************************************************************
* Determining the weight of individual studies (Random Effects)
******************************************************************************
metareg r seR , wsse(seR)
scalar tau2 =  e(tau2)
gen revarR = varR + tau2
gen reweightR = 1/revarR
gen rR = r*reweightR
collapse (sum) rR reweightR, by (id)
gen avgR = rR/reweightR
gen avgsterrR = sqrt(1/reweightR)
sort id
metan avgR avgsterrR, random lcols(id) nobox
gen studyweight = _WT
summ studyweight, detail
gsort -studyweight
//graph save forrest, replace
*/



*-----------------------*
*   TABLE 4: FAT/PET    *
*-----------------------*

// Calculating study weights
bysort id: egen numberests = count(id)
tabulate numberests 
gen weight = 1/numberests


// Generating transformed variables for FE 
gen feprecisionR = 1/seR
gen fetstatR = r/seR

// Fixed Effects without SE (no correction for publication bias)
//NOTE: If the coefficient on the constant term is significant, that 
//is evidence of publication bias
//This regression gives equal weight to each observation
regress fetstatR feprecisionR,  noc vce(cluster id)

//This regression gives equal weight to each study
regress fetstatR feprecisionR [pweight = weight], noc vce(cluster id)

// Fixed Effects with SE
//NOTE: If the coefficient on the constant term is significant, that 
//is evidence of publication bias
// Fixed Effects
//This regression gives equal weight to each observation
regress fetstatR feprecisionR, vce(cluster id)

//This regression gives equal weight to each study
regress fetstatR feprecisionR [pweight = weight], vce(cluster id)

// Generating transformed variables for RE
metareg r seR , wsse(seR)
scalar tau2 =  e(tau2)
gen revarR = varR + tau2
gen reseR = sqrt(revarR)
gen reprecisionR = 1/reseR
gen retstatR = r/reseR
gen repubbiasR = seR/reseR

// Random Effects without SE (no correction for publication bias)
//NOTE: If the coefficient on the constant term is significant, that 
//is evidence of publication bias
//This regression gives equal weight to each observation
regress retstatR reprecisionR,  noc vce(cluster id)

//This regression gives equal weight to each study
regress retstatR reprecisionR [pweight = weight], noc vce(cluster id)

// Random Effects with SE
//This regression gives equal weight to each observation
regress retstatR repubbiasR reprecisionR , noc vce(cluster id)

//This regression gives equal weight to each study
regress retstatR repubbiasR reprecisionR [pweight = weight], noc vce(cluster id)



log close
 